using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FeedAntGame.Control;
using FeedAntGame.Sprite.Event;
using FeedAntGame.Sprite;



namespace FeedAntGame.Component
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class MainMenu : Microsoft.Xna.Framework.DrawableGameComponent
    {
        
        SpriteBatch spriteBatch;
        Texture2D background;

        List<BaseSprite> spriteList = new List<BaseSprite>();
        List<EventSprite> buttonList = new List<EventSprite>();
        AutomatedSprite clound;

        //sound
        Cue currentCue = null; // hold track is playing in main menu 

        public MainMenu(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: AddOneCandyFeed your initialization code here
            base.Initialize();
        }

        protected override void LoadContent()
        {
            // TODO: AddOneCandyFeed your load content code here
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            background = Game.Content.Load<Texture2D>(@"Images\mainmenubackground");
            //
            // Load animation resource
            //
            spawnClound();
            //
            //--------------------- Load button ------------------------------
            //
            // Quit Button
            //
            Button quitButton = new Button(Game, Button.TYPE.QUIT, 
                new Vector2(Game.Window.ClientBounds.Width * 2 / 6 + 115, Game.Window.ClientBounds.Height * 3 / 5 + 120));
            EventSprite quitSprite = (EventSprite)quitButton.Sprite;
            buttonList.Add(quitSprite);
            quitSprite.Click += new EventSprite.ClickButton(this.onQuitClick);
            quitSprite.Hover += new EventSprite.HoverButton(this.onShowHand);
            //
            // New game Button
            //
            Button newGameButton = new Button(Game, Button.TYPE.NEW_GAME,
                new Vector2(365 * Game.Window.ClientBounds.Width / 860, 210 * Game.Window.ClientBounds.Height/600));
            EventSprite newGameSprite = (EventSprite)newGameButton.Sprite;
            buttonList.Add(newGameSprite);
            newGameSprite.Click += new EventSprite.ClickButton(this.onNewGameClick);
            newGameSprite.Hover += new EventSprite.HoverButton(this.onShowHand);
            //
            // Option Button
            //
            Button optionButton = new Button(Game, Button.TYPE.OPTION,
                new Vector2(Game.Window.ClientBounds.Width * 2 / 6 + 100, Game.Window.ClientBounds.Height * 2 / 5 +70));
            EventSprite optionSprite = (EventSprite)optionButton.Sprite;
            buttonList.Add(optionSprite);
            optionSprite.Click += new EventSprite.ClickButton(this.onOptionClick);
            optionSprite.Hover += new EventSprite.HoverButton(this.onShowHand);
            //
            // High scores Button
            //
            Button hsButton = new Button(Game, Button.TYPE.HIGH_SCORES,
                new Vector2(Game.Window.ClientBounds.Width * 2 / 6 + 85, Game.Window.ClientBounds.Height * 2 / 5 + 160));
            EventSprite hsSprite = (EventSprite)hsButton.Sprite;
            buttonList.Add(hsSprite);
            hsSprite.Click += new EventSprite.ClickButton(this.onHighScoresClick);
            hsSprite.Hover += new EventSprite.HoverButton(this.onShowHand);

            base.LoadContent();
        }
        
        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: AddOneCandyFeed your update code here            
            
            //update all sprite in sprite list
            for (int i = 0; i < buttonList.Count; i++)
            {
                EventSprite s = buttonList.ElementAt(i);
                s.Update(gameTime, Game.Window.ClientBounds);
                if (s.IsPlaySoundHover())
                {
                    ((GameFA)Game).PlayCue(s.collisionCueName);
                }
                                
            }
            for (int i = 0; i < spriteList.Count; i++)
            {
                BaseSprite s = spriteList.ElementAt(i);
                s.Update(gameTime, Game.Window.ClientBounds);
                if (s.Equals(clound))
                {
                    if (s.IsOutOfBounds(Game.Window.ClientBounds))
                    {
                        spawnClound();
                    }
                }
            }
            base.Update(gameTime);
        }

       

        public override void Draw(GameTime gameTime)
        {
            //  TODO: AddOneCandyFeed your draw code here

            GraphicsDevice.Clear(Color.AliceBlue);
                        
            spriteBatch.Begin();

            // Draw bg 
            spriteBatch.Draw(background, new Rectangle(0, 0, Game.Window.ClientBounds.Width, Game.Window.ClientBounds.Height),
                Color.White);
            
            // Draw sprite list
            foreach (BaseSprite s in spriteList)
                s.Draw(gameTime, spriteBatch);

            
            // Draw menu button list
            foreach (EventSprite button in buttonList)
            {
                button.Draw(gameTime, spriteBatch);
            }

            spriteBatch.End();

            base.Draw(gameTime);
        }

        /************************************************************************/
        /*                      Event                                                                     */
        /************************************************************************/

        private void accountClick()
        {

        }
        private void onShowHand()
        {
            ((GameFA)Game).Cursor.State = Cursor.STATE_TYPE.HAND;
        }
        private void onQuitClick()
        {
            ((GameFA)Game).Exit();
        }

        private void onNewGameClick()
        {
            ((GameFA)Game).GoToPlayGame();           
            //((GameFA)Game).OpenPlayAgainDlg();
        }

        private void onOptionClick()
        {
            ((GameFA)Game).OpenOptionDlg();
        }

        private void onHighScoresClick()
        {
            ((GameFA)Game).OpenHighScoreDlg();
        }
        protected override void OnVisibleChanged(object sender, EventArgs args)
        {
            if (this.Visible == true)
            {
                //play theme song: UNDERFX
                currentCue = ((GameFA)Game).PlayCue("dancer");
            }
            else
            {
                ((GameFA)Game).StopCue(currentCue, AudioStopOptions.Immediate);
            }
            base.OnVisibleChanged(sender, args);
        }
        /************************************************************************/
        /*                      Methods                                                                     */
        /************************************************************************/
        private void spawnClound()
        {
            Texture2D texture = Game.Content.Load<Texture2D>(@"Images\clound");
            Random rnd = ((GameFA)Game).rnd;
            if (rnd.Next() % 2 == 1)
            {
                clound = new AutomatedSprite(
                texture, new Vector2(texture.Width * -1, 0), new Point(375, 80), 0, Point.Zero, new Point(1, 1),
                new Vector2(0.5f, 0), "", 0, 1);
                spriteList.Add(clound);
            }
            else
            {
                clound = new AutomatedSprite(
                texture, new Vector2(Game.Window.ClientBounds.Width, 0), new Point(375, 80), 0, Point.Zero, new Point(1, 1),
                new Vector2(-1*0.5f, 0), "", 0, 1);
                spriteList.Add(clound);
            }
            
        }
    }
}
